#!/data/data/com.termux/files/usr/bin/bash -e


# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[1;31m'
BLUE='\033[1;34m'
NC='\033[0m'

# 定义变量
REPO1="$HOME/arm64"
REPO2="$HOME/arm64-2"
TARGET_DIR="$HOME/kalifs_files"
OUTPUT_ARCHIVE="$HOME/kalifs-arm64-full.tar.xz"


#获取设备的 cpu 架构
checksysinfo() {
    CPU_ABI=$(getprop ro.product.cpu.abi)
	case "$CPU_ABI" in
		arm64-v8a)
			echo -e "${GREEN} [*] 当前 cpu 架构为：$CPU_ABI ${NC}"
            SETARCH=arm64
			;;
		*)
			echo -e "${RED} [*] 当前 cpu 架构为：$CPU_ABI  暂不支持该架构${NC}"
            echo -e "${BLUE} [*] 微信公众号: ${yellow}地球村的一枝花\n ${NC}"
            exit
			;;
	esac
    DESTINATION="$HOME/kali-$SETARCH"
}


#清理旧目录
cleanup_old_dirs(){
	echo -e "${GREEN} [*] 清理旧数据...${NC}"
	rm -rf "$REPO1" "$REPO2" "$TARGET_DIR"
}

# 获取系统文件
clone_repos() {
    echo -e "${GREEN}[*] 开始获取系统文件...${NC}"
     git clone https://gitee.com/zhang-955/arm64.git "$REPO1" || {
        echo -e "${RED} [*] arm64 获取失败${NC}"
        echo -e "${BLUE} [*] 微信公众号: 地球村的一枝花${NC}"
        exit 1
    }

    git clone https://gitee.com/zhang-955/arm64-2.git "$REPO2" || {
        echo -e "${RED} [*] arm64-2 获取失败${NC}"
        echo -e "${BLUE} [*] 微信公众号: 地球村的一枝花${NC}"
        exit 1
    }
}

# 查找目标文件
find_files() {
    echo -e "${GREEN} [*] 查找 kalifs-arm64-full.tar.xz 前缀的文件...${NC}"
    mkdir -p "${TARGET_DIR}"
    find "$REPO1" "$REPO2" -type f -name 'kalifs-arm64-full.tar.xz*' -exec cp {} "$TARGET_DIR/" \;
    
    FOUND_FILES=("$TARGET_DIR"/kalifs-arm64-full.tar.xz*)
    if [ ${#FOUND_FILES[@]} -eq 0 ]; then
        echo -e "${YELLOW}[!] 未找到任何匹配的文件，请公众号截图反馈${NC}"
        echo -e "${BLUE} [*] 微信公众号: ${yellow}地球村的一枝花\n ${NC}"
        exit 1
    fi

    echo -e "${GREEN} [*] 已找到以下文件：${NC}"
    ls -lh "$TARGET_DIR"
}

# 打包系统文件
package_files() {
    echo -e "${GREEN} [*] 正在打包系统文件...${NC}"
    cd "$TARGET_DIR" || { echo -e "${RED} 路径：$TARGET_DIR 不存在 ${NC}"; exit 1; }
    cat "$TARGET_DIR/"* > "$OUTPUT_ARCHIVE" || { echo -e "${RED} 文件合并出错 ${NC}"; exit 1; }
    echo -e "${GREEN}✅ 打包完成：$OUTPUT_ARCHIVE${NC}"
    echo 
}


# 安装依赖程序
checkdeps() {
	echo -e "${GREEN} [*] 正在安装依赖程序...${NC}"
	echo -e "${GREEN} [*] 获取软件列表 ..."
	apt update -y 
	echo -e "${GREEN} [*] 检查依赖安装情况..."

	for i in proot tar; do
		if [ -e $PREFIX/bin/$i ]; then
			echo -e "${GREEN}  • $i 已安装"
		else
			echo -e "${GREEN} 正在安装 ${i}..."
			DEBIAN_FRONTEND=noninteractive apt install -y $i || {
				printf "$RED"
				echo " [*] 错误: 请检查你的网络是否正常，或apt是否安装，程序已结束..."
				printf "$NC"
				exit 1
			}
		fi
	done
}

# 校验文件完整性
checkintegrity() {
    local path="$1"
    cd "$HOME"
	printf "\n${GREEN} [*] 检查文件完整性...\n"
    printf "\n${GREEN} sha512sum路径：${REPO1}\n"
    printf "\n${GREEN} 当前路径：$(pwd)\n\n"
	printf "$NC"
	if sha512sum -c "$path" > /dev/null 2>&1; then
        printf "${GREEN} [+] ✅校验通过：文件完整\n${NC}"
    else
        printf "${RED} [-] 校验失败：文件可能损坏，请重新下载\n${NC}"
        exit 1
    fi
}

# 构建系统
extract() {

    printf "\n${GREEN} [*] 构建系统...${NC}\n\n"
    proot --link2symlink tar -xf "$OUTPUT_ARCHIVE" -C "$HOME" 2> /dev/null || :
    printf "\n${GREEN} [*] ✅构建完成${NC}\n\n"
}

# 构建启动脚本
createloginfile() {
    printf "\n${GREEN} [*] 构建启动脚本...${NC}\n\n"
	bin=${PREFIX}/bin/startkali
	cat > $bin <<- EOM
#!/data/data/com.termux/files/usr/bin/bash -e
unset LD_PRELOAD
if [ ! -f $DESTINATION/root/.version ]; then
    touch $DESTINATION/root/.version
fi
user=kali
home="/home/\$user"
LOGIN="sudo -u \$user /bin/bash"
if [[ ("\$#" != "0" && ("\$1" == "-r")) ]]; then
    user=root
    home=/\$user
    LOGIN="/bin/bash --login"
    shift
fi

cmd="proot \\
    --link2symlink \\
    -0 \\
    -r ${DESTINATION} \\
    -b /dev \\
    -b /sys \\
    -b /proc \\
    -b $DESTINATION\$home:/dev/shm \\
    -b /sdcard \\
    -b $HOME \\
    -w \$home \\
    /usr/bin/env -i \\
    HOME=\$home TERM="\$TERM" \\
    LANG=\$LANG PATH=/bin:/usr/bin:/sbin:/usr/sbin \\
    \$LOGIN"

args="\$@"
if [ "\$#" == 0 ]; then
    exec \$cmd
else
    \$cmd -c "\$args"
fi
EOM
	chmod 700 $bin
    printf "\n${GREEN} [*] ✅构建完成${NC}\n\n"
}


# 系统配置
finalwork() {
	printf "\n${GREEN} [*] 初始化系统配置...${NC}\n\n"
	DESTINATION=$DESTINATION SETARCH=$SETARCH bash $HOME/clone/AutoInstallKali/finaltouchup.sh
    printf "\n${GREEN} [*] ✅初始化完成${NC}\n\n"
} 


# 主函数
main() {
    clear
    printf "\n${YELLOW} 更新时间：2025-06-12\n\n"

    checksysinfo
    cleanup_old_dirs
    clone_repos
    find_files
    package_files
    checkdeps
    checkintegrity "$REPO1/kalifs-arm64-full.sha512sum"
    extract
    createloginfile
    finalwork

    cleanup_old_dirs
}

# 运行主函数
main


printf "\n$BLUE 安装完成 使用$YELLOW startkali $BLUE进入Linux\n\n"
printf "${BLUE} kali: ${YELLOW}kali\n\n"
printf "${BLUE} root: ${YELLOW}toor\n\n"
printf "$BLUE [*] 微信公众号: ${YELLOW}地球村的一枝花\n"
printf "$NC"
